package cn.iocoder.yudao.module.biz.service.staff;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.dal.mysql.staff.CustomBizStaffMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.staff.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.staff.BizStaffDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.staff.BizStaffConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.staff.BizStaffMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 员工 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizStaffServiceImpl implements BizStaffService {

    @Resource
    private BizStaffMapper staffMapper;

    @Resource
    private CustomBizStaffMapper customBizStaffMapper;

    @Override
    public String createStaff(BizStaffCreateReqVO createReqVO) {
        // 设置单位
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        createReqVO.setDeptId(loginUser.getDeptId());
        // 插入
        BizStaffDO staff = BizStaffConvert.INSTANCE.convert(createReqVO);
        staffMapper.insert(staff);
        // 返回
        return staff.getId();
    }

    @Override
    public void updateStaff(BizStaffUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateStaffExists(updateReqVO.getId());
        // 更新
        BizStaffDO updateObj = BizStaffConvert.INSTANCE.convert(updateReqVO);
        staffMapper.updateById(updateObj);
    }

    @Override
    public void deleteStaff(String id) {
        // 校验存在
        this.validateStaffExists(id);
        // 删除
        staffMapper.deleteById(id);
    }

    private void validateStaffExists(String id) {
        if (staffMapper.selectById(id) == null) {
            throw exception(STAFF_NOT_EXISTS);
        }
    }

    @Override
    public BizStaffDO getStaff(String id) {
        return staffMapper.selectById(id);
    }

    @Override
    public BizStaffDO getStaffByCode(String staffCode) {
        return staffMapper.selectOne(BizStaffDO::getStaffCode, staffCode);
    }

    @Override
    public List<BizStaffDO> getStaffList(Collection<String> ids) {
        return staffMapper.selectBatchIds(ids);
    }

    @Override
    public List<BizStaffDO> getStaffListAll() {
        QueryWrapperX<BizStaffDO> queryWrapperX = new QueryWrapperX<>();
        return customBizStaffMapper.list(queryWrapperX);
    }

    @Override
    public PageResult<BizStaffDO> getStaffPage(BizStaffPageReqVO pageReqVO) {
        //return staffMapper.selectPage(pageReqVO);
        QueryWrapperX<BizStaffDO> queryWrapperX = new QueryWrapperX<>();
        queryWrapperX
                .eqIfPresent("bs.staff_dept_id", pageReqVO.getStaffDeptId());
        Page<BizStaffDO> page = new Page(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        //List<BizStaffDO> list = customBizStaffMapper.list(page, queryWrapperX);
        List<BizStaffDO> list = customBizStaffMapper.list(page, queryWrapperX);
        long total = customBizStaffMapper.pageCount(queryWrapperX);
        PageResult<BizStaffDO> pageResult = new PageResult(list, total);
        return pageResult;
    }

    @Override
    public List<BizStaffDO> getStaffList(BizStaffExportReqVO exportReqVO) {
        return staffMapper.selectList(exportReqVO);
    }

    @Override
    public Long getStaffCount() {
        return staffMapper.selectCount(new QueryWrapperX<BizStaffDO>().eqIfPresent("deleted",0));
    }

}
